//
//  ESTWorkbenchController.m
//  ESTSCRMAPP
//
//  Created by 李贤立 on 2018/9/28.
//  Copyright © 2018年 Capfer. All rights reserved.
//

#import "ESTWorkbenchController.h"
#import "ESTWorkbenchCell.h"
#import "ESTActivityViewController.h"

@interface ESTWorkbenchController ()<UITableViewDelegate, UITableViewDataSource>
/**
 *  TableView
 */
@property(nonatomic, strong) TYZTableView *tableView;
/**
 *  记录滑动量
 */
@property(nonatomic, assign) CGFloat offSetY;
/**
 *  第一部分文字数组
 */
@property(nonatomic, strong) NSArray *firstPartTextArray;
/**
 *  第一部分数值数组
 */
@property(nonatomic, strong) NSArray *firstPartValueArray;
/**
 *  日报还是月报
 */
@property(nonatomic, assign) BOOL isMonth;
/**
 *  可以抢的线索
 */
@property(nonatomic, strong) NSArray *clueArray;
/**
 *  第三部分文字数组
 */
@property(nonatomic, strong) NSArray *thirdPartTextArray;
/**
 *  第三部分数值数组
 */
@property(nonatomic, strong) NSArray *thirdPartValueArray;
/**
 *  第四部分的数据源
 */
@property(nonatomic, strong) NSArray *fourthPartArray;
@end

@implementation ESTWorkbenchController
#pragma mark - 生命周期方法
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTitleView];
    [self setupTableView];
}
- (void)setupTitleView {
    TYZView *titleView = [[TYZView alloc] init];
    [self.view addSubview:titleView];
    [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.view);
        make.height.mas_equalTo(DE_NavigationHeight);
    }];
    titleView.backgroundColor = NavigationColor;
    TYZLabel *nameLabel = [[TYZLabel alloc] init];
    [titleView addSubview:nameLabel];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(titleView).offset(-10);
        make.left.equalTo(titleView).offset(20);
    }];
    nameLabel.text = UserInfo.EMP_INFO.firstObject.DLR_NAME;
    nameLabel.textColor = TYZWhiteColor;
    nameLabel.font = [UIFont boldSystemFontOfSize:CoreInfo.fontSize * 1.2];
    
    TYZButton *messageButton = [[TYZButton alloc] init];
    [titleView addSubview:messageButton];
    [messageButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(nameLabel);
        make.right.equalTo(titleView).offset(-20);
    }];
    [messageButton setBackgroundImage:[UIImage imageNamed:@"message_redPoint"] forState:UIControlStateNormal];
    [messageButton addTarget:self action:@selector(didClickMessageButton) forControlEvents:UIControlEventTouchUpInside];
    
    TYZButton *searchButton = [[TYZButton alloc] init];
    [titleView addSubview:searchButton];
    [searchButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(nameLabel);
        make.right.equalTo(messageButton.mas_left).offset(-10);
    }];
    [searchButton setBackgroundImage:[UIImage imageNamed:@"search_icon"] forState:UIControlStateNormal];
    [searchButton addTarget:self action:@selector(didClickSearchButton) forControlEvents:UIControlEventTouchUpInside];
}
- (void)setupTableView {
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(DE_NavigationHeight);
        make.left.right.bottom.equalTo(self.view);
    }];
    [self setupTableViewMj_header];
}
- (void)setupTableViewMj_header {
    WEAKSELF
    self.tableView.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
        //        [weakSelf refreshHomeData];
        [weakSelf.tableView.mj_header endRefreshing];
    }];
    MJRefreshStateHeader *stateHeader = (MJRefreshStateHeader *)self.tableView.mj_header;
    stateHeader.textColor = TYZWhiteColor;
}
#pragma mark - actions
- (void)didClickMessageButton {
    
}
- (void)didClickSearchButton {
    
}
- (void)didClickRefreshClueButton:(TYZButton *)button {
    
}
- (void)didClickGetClueButton:(TYZButton *)button {
    
}
#pragma mark - UITableViewDelegate, UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return ESTWorkbenchCellTitleHeight2 + ESTWorkbenchCellChildHeight1 * 2 + ESTWorkbenchCellPadding;
    }
    if (indexPath.section == 1) {
        return ESTWorkbenchCellSecondHeight;
    }
    if (indexPath.section == 2) {
        return ESTWorkbenchCellTitleHeight1 + ESTWorkbenchCellChildHeight2;
    }
    if (indexPath.section == 3) {
        return ESTWorkbenchCellChildHeight1 * (self.fourthPartArray.count / 3 + 1);
    }
    return 44;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    }
    return Value_Height_PartPadding;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    TYZView *view = [[TYZView alloc] init];
    view.backgroundColor = ESTBackgroundColor;
    return view;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ESTWorkbenchCell *cell = [ESTWorkbenchCell creatWorkbenchCell:tableView section:indexPath.section];
    WEAKSELF
    if (indexPath.section == 0) {
        cell.title = [@"我的业绩" S];
        cell.selectedNumber = self.isMonth;
        cell.nameArray = self.firstPartTextArray;
        cell.valueArray = self.firstPartValueArray;
        [cell setDidSelectedChildBlock:^(NSInteger integer) {
            [weakSelf.tableView reloadData];
        }];
        [cell setDidSelectedDateTypeBlock:^(NSInteger integer) {
            weakSelf.isMonth = integer;
            [weakSelf.tableView reloadData];
        }];
    } else if (indexPath.section == 1) {
        cell.title = [@"抢线索" S];
        cell.clueArray = self.clueArray;
        [cell setDidClickGetClueButton:^(NSInteger integer) {
            
        }];
        [cell setDidClickRefreshButton:^(TYZButton *refreshButton) {
            
        }];
    } else if (indexPath.section == 2) {
        cell.title = [@"今日待办" S];
        cell.nameArray = self.thirdPartTextArray;
        cell.valueArray = self.thirdPartValueArray;
        [cell setDidSelectedChildBlock:^(NSInteger integer) {
            [weakSelf.tableView reloadData];
        }];
    } else if (indexPath.section == 3) {
        cell.dataArray = self.fourthPartArray;
        [cell setDidSelectedChildBlock:^(NSInteger integer) {
            if (integer == 8) {
                [weakSelf.navigationController pushViewController:[[ESTActivityViewController alloc] init] animated:YES];
            }
        }];
    }
    return cell;
}
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat offSetY = scrollView.contentOffset.y;
    self.offSetY = offSetY;
    if (offSetY < 0 && self.offSetY > 0) {
        self.offSetY = offSetY;
    }
    if (offSetY >= 0 && self.offSetY < 0) {
        self.offSetY = offSetY;
    }
}
#pragma mark - setter & getter
- (TYZTableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[TYZTableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}
- (void)setOffSetY:(CGFloat)offSetY {
    _offSetY = offSetY;
    if (offSetY <= 0) {
        self.tableView.backgroundColor = NavigationColor;
    } else {
        self.tableView.backgroundColor = ESTBackgroundColor;
    }
}
- (NSArray *)firstPartTextArray {
    if (_firstPartTextArray == nil) {
        _firstPartTextArray = @[
                                [@"邀约" S],
                                [@"到店" S],
                                [@"留档" S],
                                [@"试驾" S],
                                [@"跟进" S],
                                [@"订单" S]
                                ];
    }
    return _firstPartTextArray;
}
- (NSArray *)firstPartValueArray {
    if (_firstPartValueArray == nil) {
        _firstPartValueArray = @[
                                 @"0",
                                 @"0",
                                 @"0",
                                 @"0",
                                 @"0",
                                 @"0"
                                 ];
    }
    return _firstPartValueArray;
}
- (NSArray *)thirdPartTextArray {
    if (_thirdPartTextArray == nil) {
        _thirdPartTextArray = @[
                                [@"计划跟进" S],
                                [@"计划到店" S],
                                [@"计划交车" S],
                                [@"客户关怀" S]
                                ];
    }
    return _thirdPartTextArray;
}
- (NSArray *)thirdPartValueArray {
    if (_thirdPartValueArray == nil) {
        _thirdPartValueArray = @[
                                 @"0",
                                 @"0",
                                 @"0",
                                 @"0"
                                 ];
    }
    return _thirdPartValueArray;
}
- (NSArray *)fourthPartArray {
    if (_fourthPartArray == nil) {
        _fourthPartArray = @[
                             @[@"form_tile_flow_icon",  [@"流量补录" S]],
                             @[@"form_tile_test_drive_icon",  [@"试乘试驾" S]],
                             @[@"form_tile_client_come_icon",  [@"客户到店" S]],
                             @[@"form_tile_report_manage_icon",  [@"报表统计" S]],
                             @[@"form_tile_sms_templates_icon",  [@"车辆库存" S]],
                             @[@"form_tile_sms_templates_icon",  [@"短信模版" S]],
                             @[@"form_tile_car_info_icon",  [@"车型资料" S]],
                             @[@"form_tile_knowledge_icon",  [@"知识库" S]],
                             @[@"form_tile_activity_icon",  [@"最新活动" S]],
                             @[@"form_tile_sales_ranking_icon",  [@"销冠排行" S]],
                             @[@"form_title_month_target_icon",  [@"月度目标" S]]
                             ];
    }
    return _fourthPartArray;
}
@end
